

<title>管理员管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">控制台</a>
    <a><cite>管理员管理</cite></a>
  </div>
</div>

<div class="layui-fluid">   
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="back-admin-list-search">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">登录名</label>
          <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">角色</label>
          <div class="layui-input-block">
            <select name="role_id" id="back-admin-list-search-role-select">
              <option value="">全部</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-block">
            <select name="status">
              <option value="">全部</option>
              <option value="1">启用</option>
              <option value="0">禁用</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="back-admin-list-search-submit">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
      </div>
      
      <table id="back-admin-list" lay-filter="back-admin-list"></table>  
      <script type="text/html" id="buttonSwitch">
        <input type="checkbox" name="" lay-skin="switch" lay-text="开启|禁用" lay-filter="status" value="{{d.id}}" 
        {{ d.status == 1 ? 'checked' : '' }}>
      </script>
      <script type="text/html" id="back-admin-list-operation">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('administrator', layui.factory('administrator')).use(['admin', 'administrator', 'table'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form
  ,setter = layui.setter;

  // 角色下拉框
  admin.req({
    url: setter.api_back_role_list
    ,data: {status:2}
    ,done: function(res){
      $.each(res.data.list, function (index, item) {
        status_txt = item.status == 0 ? ' [已禁用]' : '';
        $('#back-admin-list-search-role-select').append(new Option(item.name + status_txt, item.id));// 下拉菜单里添加元素
      });
      form.render(null, 'back-admin-list-search');
    }
  });

  //监听搜索
  form.on('submit(back-admin-list-search-submit)', function(data){
    var field = data.field;
    //执行重载
    table.reload('back-admin-list', {
      where: field
    });
  });

  //监听 状态switch 
  form.on('switch(status)', function(data){
    if(data.elem.checked == true){
      admin.req({
        url:setter.api_back_admin_update,
        data:{'id':data.value,'status':1},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-admin-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-admin-list'); //重载表格
        }
      });
    }else{
      admin.req({
        url:setter.api_back_admin_update,
        data:{'id':data.value,'status':0},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-admin-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-admin-list'); //重载表格
        }
      });
    }
  });

  //事件
  var active = {
    add: function(){
      admin.popup({
        title: '添加管理员'
        ,area: admin.screen() < 2 ? ['100%','400px'] : ['450px','400px']
        ,id: 'back-admin-add-popup'
        ,success: function(layero, index){
          view(this.id).render('administrator/form').done(function(){
            // 角色下拉框
            admin.req({
              url: setter.api_back_role_list
              ,data: {status:1}
              ,done: function(res){
                $.each(res.data.list, function (index, item) {
                  $('#back-admin-form-role-select').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                });
                form.render(null, 'back-admin-form');
              }
            });
            //监听提交
            form.on('submit(back-admin-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_admin_create
                ,data: field
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭 
                  table.reload('back-admin-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  }  
  $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>